//
//  CollectViewController.m
//  GSG
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CollectViewController.h"
#import "Code.h"
#import "Singleton.h"
#import "PlayerViewController.h"
#import"singleSong.h"
@interface CollectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)UITableView *cllectTableView;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)NSMutableArray *modelArr;
@end

@implementation CollectViewController
- (void)dealloc
{
    [_cllectTableView release];
    [_arr release];
    [_modelArr release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
        self.modelArr = [NSMutableArray array];
        
    }
    
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing.png"]];
    
    self.cllectTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 60 / 667) style:UITableViewStylePlain];
    self.cllectTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.cllectTableView];
    [_cllectTableView release];
    
    self.cllectTableView.delegate=self;
    self.cllectTableView.dataSource=self;
    //    self.cllectTableView.rowHeight=100;
    [self data];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"删除"] style:UIBarButtonItemStylePlain target:self action:nil];
    
    
}

- (void)data{
    [[Singleton shareSingleton] openDB];
    [[Singleton shareSingleton] createTable];
    NSMutableArray *arr = [[Singleton shareSingleton] selectLand];
    
    for (Code *code in arr) {
        [self.arr addObject:code.songName];
    }
    
    
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arr.count;
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse=@"reuse";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = self.arr[indexPath.row];
    return  cell;
}



// 重写系统按钮
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.cllectTableView setEditing:editing animated:animated];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.arr removeObjectAtIndex:indexPath.row];
        [self.cllectTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
        //        [[Singleton shareSingleton] dropTable];
        
        NSMutableArray *arr = [[Singleton shareSingleton] selectLand];

        Code *model = arr[indexPath.row];

        [[Singleton shareSingleton] deleteLand:model.number];
        
        
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSMutableArray *arr = [[Singleton shareSingleton] selectLand];
    PlayerViewController *video = [[PlayerViewController alloc] init];
    for (Code *model in arr) {
        SongModel *song=[[SongModel alloc]init];
        song.song_name=model.songName;
        song.singer_name=model.singerName;
        song.url=model.url;
        [self.modelArr addObject:song];
    }
//    video.arr= self.modelArr;
//    video.num = indexPath.row;
//    [self.navigationController pushViewController:video animated:YES];
//    
//    [video release];
    [singleSong shareSingleSong].arrSong=self.modelArr;
    [singleSong shareSingleSong].num=indexPath.row;
    
    NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
    
    [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
